﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using Gmail.Presenters;

namespace WordGmailAddIn
{
    public static class Logic
    {
        
        public static void Init()
        {
            
            Microsoft.Office.Interop.Word.Application Application = Globals.GmailAddIn.Application;
            Document ActiveDocument = Application.ActiveDocument;
            if (!ActiveDocument.Saved)
            {
                DialogResult DR = MessageBox.Show("Document is not saved. Save?", "Save", MessageBoxButtons.YesNo);
                if (DR == DialogResult.Yes)
                {
                    try
                    {
                        ActiveDocument.Save();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Oparetion failed " + e.Message);
                    }
                }
                else
                    return;
            }
            String ThisFilename = ActiveDocument.FullName;
            String DisplayFilename = ActiveDocument.Name;
           
            String TempFilename = System.IO.Path.GetTempFileName();
            Object OFilename = TempFilename as Object;
            Object M = System.Reflection.Missing.Value;
            try
            {
                // Save as temporary file
                ActiveDocument.SaveAs(ref OFilename, 
                    ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M,
                    ref M, ref M, ref M, ref M, ref M);
                OFilename = ThisFilename;

                // Save as normal file
                ActiveDocument.SaveAs(ref OFilename,
                    ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M, ref M,
                    ref M, ref M, ref M, ref M, ref M);

                // Send the temp file
                App.ShowLogin(TempFilename, DisplayFilename);
            }
            catch (Exception e)
            {
                MessageBox.Show("Oparetion failed " + e.Message);
            }
        }
    }
}
